<?php

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);
if (empty($this->Param['option'])) {
    $where = array(
        '`bk_status` = ?' => 'ok',
        '`bk_id` IN (?)' => empty($member_info['u_bank']) ? array(0) : explode(',', $member_info['u_bank'])
    );
    if($this->Param['oid'] > 0){
        $order = $OrderHelper->orderRow($this->Param['oid']);
        $cert_list = $OrderHelper->certList(array('`ct_oid` =?' =>$this->Param['oid'],'`ct_status`=?' =>'ok'));
        $count = count($cert_list);
        $g = 0;
        foreach($cert_list as $k => $v){
            if($v['ct_key'] == 'guarantor'){
                $order['guarantor'][$g] = $v;
                $g++;
            }else{
                $order[$v['ct_key']] = $v;
            }
        }
        $bank = $order['o_bank'];
        $this->Assign('order',$order);
    }else{
        $bank = $BankHelper->bankAll($where);
    }
    $this->Assign('bank', $bank);
    echo $this->GetView('input.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            $params = $this->Param;
            $params['uid'] = $this->UserConfig['Id'];
            
            $member = $MemberHelper->getMember($params['uid']);
            $params['o_uname'] = $member['u_realname'];
            $params['o_branch_type'] = $member['u_dp_type'];
            $params['o_branch_id'] = $member['u_dp_id'];
            $params['o_branch'] = $member['u_dpname'];
            
            if ($params['o_bank_id']) {
                $bank_row = $BankHelper->bankRow($params['o_bank_id']);
                $params['o_bank'] = $bank_row['bk_name'];
            }
            $params['o_uid'] = $params['uid'];

            $result = $OrderHelper->orderSave($params);
            if ($result['success']) {
                $msg = $MessageHelper->sendSingle(array(
                    'm_uid' => $this->UserConfig['Id'],
                    'm_title' => '征信录入成功',
                    'm_name' => $member_info['u_realname'],
                    'm_content' => '征信录入提交成功，等待征信查询通过',
                    'm_dateline' => NOW_TIME,
                    'm_oid' => $result['msg'],
                    'm_info' => serialize(array('status' => '征信提交', 'code' => $OrderHelper->statusChange('征信提交')))
                ));
                $result = $OrderHelper->updateAttachoid($result['msg'],$params['temp_id']);
                $MemberHelper->saveLog($this->UserConfig, '征信录入', json_encode($this->Param), $this->Controller . '_' . $this->Action. ($result>100?'_'.$result:''));
                ErrorMsg::Debug('处理成功',TRUE);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'replace':
        $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
        $result = $OrderHelper->creditBack($this->Param,$oid,$member_info);
        if($result['success']){
            $MemberHelper->saveLog($this->UserConfig, '征信过期退回', json_encode($this->Param), $this->Controller . '_' . $this->Action);
            ErrorMsg::Debug('退回成功', TRUE);
        }else{
            ErrorMsg::Debug('退回失败'.$result['msg']);
        }
        break;
        default :
    }
}